import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import Login from '@/woniubook/Login'
import Home from '@/woniubook/Home'
import WelCome from '@/woniubook/WelCome'

import Permission from '@/woniubook/system/Permission'  //Permission
import Role from '@/woniubook/system/Role'
import Manager from '@/woniubook/system/Manager'

import Book from '@/woniubook/book/Book'
import Order from '@/woniubook/order/Order'

import TreeDemo from '@/woniubook/TreeDemo'

const routes = [
  {
    path:"/",
    name:"Login",
    component:Login
  },
  {
    path:"/Login",
    name:"Login",
    component:Login
  },
  {
    path:"/Home",
    name:"Home",
    component:Home,
    children:[
      {
        path:"/",
        name:"WelCome",
        component:WelCome
      },
      {
        path:"/permission",
        name:"permission",
        component: Permission
      },
      {
        path:"/sysrole",
        name:"Role",
        component: Role
      },
      {
        path:"/testTree",
        name:"TreeDemo",
        component: TreeDemo
      },
      {
        path:"/managerList",
        name:"Manager",
        component: Manager
      },
      {
        path:"/book",
        name:"Book",
        component: Book
      },
      {
        path:"/order",
        name:"Order",
        component: Order
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//拦截 路由
/*
   1. to 你要访问的路由对象
   2. from 从哪个路由过来的
   3. next  控制你的访问
 */
router.beforeEach((to,form,next)=>{
  console.log("访问之前，执行。。。")
  let path = to.path ;
  if (path == "/" || path == "/Login") {
    //放行
    next() ;
  }

  let token = sessionStorage.getItem("token");
  if (token == null){
    //访问 登录页面
    next("/") ;
  }else {
    next()
  }
})

//路由访问之后 ，执行的函数
router.afterEach((to,from,next)=>{
  console.log("访问之后，执行。。。。。。。")
})

export default router